﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace RM2012Mobile
{
    public partial class frmTablesServing : Form
    {
        RM2012Service.RM2012Service objService = new RM2012Service.RM2012Service();
        DataSet ds;

        public static string tablesIdPublic = "NOTHING";
        public static string tablesNamePublic = "NOTHING";//
        public static string invoiceIdPublic = "NOTHING";//
        public static string menuIdPublic = "NOTHING";
        public static string menuNamePublic = "NOTHING";
        public static string objectNamePublic = "PAY";
        public static string actionPublic = "REQUEST";
        public static string numOldPublic = "NOTHING";
        public static string numNewPublic = "NOTHING";
        public static string usernamePublic = Utilities.UsernamePublic;
        public static string fullnamePublic = Utilities.FullNamePublic;
        public static string groupnamePublic = Utilities.GroupNamePublic;

        public frmTablesServing()
        {
            InitializeComponent();
        }

        private void frmTablesServing_Load(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        public void RefreshGrid()
        {
            ds = new DataSet();
            ds = objService.GetAllInvoiceNotPaid();
            gridTablesServing.DataSource = ds.Tables[0];
            if (ds.Tables[0].Rows.Count > 0)
            {
                BindDataPublic();
            }
        }

        private void gridTablesServing_Click(object sender, EventArgs e)
        {
            BindDataPublic();
        }

        public void BindDataPublic()
        {
            int rowIndex = gridTablesServing.CurrentRowIndex;
            Utilities.InvoiceIdPublic = gridTablesServing[rowIndex, 0].ToString();
            Utilities.TablesNamePublic = gridTablesServing[rowIndex, 1].ToString();
            tablesNamePublic = Utilities.TablesNamePublic;
            invoiceIdPublic = Utilities.InvoiceIdPublic;
        }

        private void menuItemClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menuItemRefresh_Click(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        private void menuItemDetail_Click(object sender, EventArgs e)
        {
            btnDetail_Click(sender, e);
        }

        private void menuItemSendRequest_Click(object sender, EventArgs e)
        {
            btnPay_Click(sender, e);
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        private void btnDetail_Click(object sender, EventArgs e)
        {
            frmInvoiceDetail frmDetail = new frmInvoiceDetail();
            frmDetail.Show();
        }

        private void btnPay_Click(object sender, EventArgs e)
        {
            ServerLoggingPay();
        }

        public void ServerLoggingPay()
        {
            Thread tClient = new Thread(new ThreadStart(PayRequestLog));
            tClient.Start();
        }

        public void PayRequestLog()
        {
            try
            {
                int rowIndex = gridTablesServing.CurrentRowIndex;
                string time = Utilities.GetNow();
                string message = Utilities.CreateMessage(tablesIdPublic, tablesNamePublic, invoiceIdPublic, menuIdPublic, menuNamePublic, objectNamePublic, actionPublic, numOldPublic, numNewPublic, usernamePublic, fullnamePublic, groupnamePublic, time);

                MyUdpClient client = new MyUdpClient();
                client.OnConnect(message);
            }
            catch
            {
                MessageBox.Show("Chưa gửi được !");
            }
        }
    }
}